/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.bridge;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.Lifecycle;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;

import com.yc.video.controller.InterVideoController;
import com.yc.video.player.InterVideoPlayer;

/**
 * 此类的目的是为了在InterControlView接口实现类中既能调用VideoPlayer的api又能调用BaseVideoController的api，并对部分api做了封装，方便使用
 *
 * @since 2021-05-10
 */
public class ControlWrapper implements InterVideoPlayer, InterVideoController {
    private static boolean landscapeFlag = false;
    private static boolean portraitFlag = true;
    private InterVideoPlayer mVideoPlayer;
    private InterVideoController mController;
    private StackLayout stackLayout;
    private int toggleState = 0;
    private Text mText;

    /**
     * ControlWrapper
     *
     * @param videoPlayer videoPlayer
     * @param controller controller
     */
    public ControlWrapper(InterVideoPlayer videoPlayer, InterVideoController controller) {
        mVideoPlayer = videoPlayer;
        mController = controller;
    }

    public static boolean isLandscapeFlag() {
        return landscapeFlag;
    }

    public static void setLandscapeFlag(boolean landscapeFlag) {
        ControlWrapper.landscapeFlag = landscapeFlag;
    }

    public static boolean isPortraitFlag() {
        return portraitFlag;
    }

    public static void setPortraitFlag(boolean portraitFlag) {
        ControlWrapper.portraitFlag = portraitFlag;
    }

    @Override
    public void setUrl(String url) {
        mVideoPlayer.setUrl(url);
    }

    @Override
    public String getUrl() {
        return mVideoPlayer.getUrl();
    }

    @Override
    public void start() {
        mVideoPlayer.start();
    }

    @Override
    public void pause() {
        mVideoPlayer.pause();
    }

    @Override
    public long getDuration() {
        return mVideoPlayer.getDuration();
    }

    @Override
    public long getCurrentPosition() {
        return mVideoPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(long pos) {
        mVideoPlayer.seekTo(pos);
    }

    @Override
    public boolean isPlaying() {
        return mVideoPlayer.isPlaying();
    }

    @Override
    public int getBufferedPercentage() {
        return mVideoPlayer.getBufferedPercentage();
    }

    @Override
    public void startFullScreen() {
        mVideoPlayer.startFullScreen();
    }

    @Override
    public void stopFullScreen() {
        mVideoPlayer.stopFullScreen();
    }

    @Override
    public boolean isFullScreen() {
        return mVideoPlayer.isFullScreen();
    }

    @Override
    public void setMute(boolean isMute) {
        mVideoPlayer.setMute(isMute);
    }

    @Override
    public boolean isMute() {
        return mVideoPlayer.isMute();
    }

    @Override
    public void setScreenScaleType(int screenScaleType) {
        mVideoPlayer.setScreenScaleType(screenScaleType);
    }

    @Override
    public void setSpeed(float speed) {
        mVideoPlayer.setSpeed(speed);
    }

    @Override
    public float getSpeed() {
        return mVideoPlayer.getSpeed();
    }

    @Override
    public long getTcpSpeed() {
        return mVideoPlayer.getTcpSpeed();
    }

    @Override
    public void replay(boolean resetPosition) {
        mVideoPlayer.replay(resetPosition);
    }

    @Override
    public void setMirrorRotation(boolean enable) {
        mVideoPlayer.setMirrorRotation(enable);
    }

    @Override
    public PixelMapElement doScreenShot() {
        return mVideoPlayer.doScreenShot();
    }

    @Override
    public int[] getVideoSize() {
        return mVideoPlayer.getVideoSize();
    }

    @Override
    public void setRotation(float rotation) {
        mVideoPlayer.setRotation(rotation);
    }

    @Override
    public void startTinyScreen() {
        mVideoPlayer.startTinyScreen();
    }

    @Override
    public void stopTinyScreen() {
        mVideoPlayer.stopTinyScreen();
    }

    @Override
    public boolean isTinyScreen() {
        return mVideoPlayer.isTinyScreen();
    }

    public int getToggleState() {
        return toggleState;
    }

    public void setToggleState(int toggleState) {
        this.toggleState = toggleState;
    }

    /**
     * 播放和暂停
     */
    public void togglePlay() {
        if (isPlaying()) {
            pause();
        } else {
            start();
        }
    }

    /**
     * 横竖屏切换，会旋转屏幕
     *
     * @param activity
     */
    public void toggleFullScreen(Ability activity) {
        if (activity == null || activity.getLifecycle().getLifecycleState() == Lifecycle.Event.ON_STOP) {
            return;
        }
        if (isFullScreen()) {
            activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
            stopFullScreen();
            ControlWrapper.defaultValue();
        } else {
            activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.LANDSCAPE);
            startFullScreen();
            setLandscapeFlag(true);
            setPortraitFlag(false);
        }
    }

    /**
     * 横竖屏切换，不会旋转屏幕
     */
    public void toggleFullScreen() {
        if (isFullScreen()) {
            stopFullScreen();
        } else {
            startFullScreen();
        }
    }

    /**
     * 横竖屏切换，根据适配宽高决定是否旋转屏幕
     *
     * @param activity
     */
    public void toggleFullScreenByVideoSize(AbilitySlice activity) {
        if (activity == null || activity.getLifecycle().getLifecycleState() == Lifecycle.Event.ON_STOP) {
            return;
        }
        int[] size = getVideoSize();
        int width = size[0];
        int height = size[1];
        if (isFullScreen()) {
            stopFullScreen();
            if (width > height) {
                activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
            }
        } else {
            startFullScreen();
            if (width > height) {
                activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.LANDSCAPE);
            }
        }
    }

    @Override
    public void startFadeOut() {
        mController.startFadeOut();
    }

    @Override
    public void stopFadeOut() {
        mController.stopFadeOut();
    }

    @Override
    public boolean isShowing() {
        return mController.isShowing();
    }

    @Override
    public void setLocked(boolean locked) {
        mController.setLocked(locked);
    }

    @Override
    public boolean isLocked() {
        return mController.isLocked();
    }

    @Override
    public void portraitPlayer(boolean isPortrait) {
        mController.portraitPlayer(isPortrait);
    }

    @Override
    public void startProgress() {
        mController.startProgress();
    }

    @Override
    public void stopProgress() {
        mController.stopProgress();
    }

    @Override
    public void hide() {
        mController.hide();
    }

    @Override
    public void show() {
        mController.show();
    }

    @Override
    public boolean hasCutout() {
        return mController.hasCutout();
    }

    @Override
    public int getCutoutHeight() {
        return mController.getCutoutHeight();
    }

    @Override
    public void destroy() {
        mController.destroy();
    }

    /**
     * 切换锁定状态
     */
    public void toggleLockState() {
        setLocked(!isLocked());
    }

    /**
     * 切换显示/隐藏状态
     */
    public void toggleShowState() {
        if (isShowing()) {
            hide();
        } else {
            show();
        }
    }

    /**
     * 传入Text
     *
     * @param text
     */
    public void setToggleText(Text text) {
        mText = text;
    }

    /**
     * 传入StackLayout
     *
     * @param stackLayout stackLayout
     */
    public void setToggleControlView(StackLayout stackLayout) {
        this.stackLayout = stackLayout;
    }

    /**
     * 显示文本重置布局高度
     */
    public void showTextResetLayout() {
        if (mText != null && stackLayout != null) {
            mText.setVisibility(Component.VISIBLE);
            ComponentContainer.LayoutConfig layoutConfig = stackLayout.getLayoutConfig();
            layoutConfig.setMargins(0, 0, 0, 0);
            stackLayout.setLayoutConfig(layoutConfig);
        }
    }

    /**
     * 隐藏文本设置布局margin为30
     *
     * @param context
     */
    public void hideTextSetLayout(Context context) {
        if (mText != null && stackLayout != null) {
            mText.setVisibility(Component.HIDE);
            ComponentContainer.LayoutConfig layoutConfig = stackLayout.getLayoutConfig();
            if (context != null) {
                int margin = AttrHelper.vp2px(31, context);
                layoutConfig.setMargins(margin, 0, 0, margin);
            } else {
                layoutConfig.setMargins(100, 0, 0, 100);
            }
            stackLayout.setLayoutConfig(layoutConfig);
        }
    }

    /**
     * 恢复默认值
     */
    public static void defaultValue() {
        landscapeFlag = false;
        portraitFlag = true;
    }
}
